package com.hundun.hddeal.controller;


import com.hundun.hddeal.dao.PayMapper;
import com.hundun.hddeal.dao.RecordMapper;
import com.hundun.hddeal.entity.AdUser;
import com.hundun.hddeal.entity.MtUser;
import com.hundun.hddeal.entity.Pay;
import com.hundun.hddeal.entity.Record;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.lmpl.AdUserServiceImpl;
import com.hundun.hddeal.service.lmpl.MtUserServiceImpl;
import com.hundun.hddeal.service.lmpl.RecordServiceImpl;
import com.hundun.hddeal.util.TokenUtilT;
import com.hundun.hddeal.vo.MoneySum;
import com.hundun.hddeal.vo.RecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;

/**
 * 记录表
 * @author 谢
 */
@Slf4j
@RestController
@RequestMapping("record")
public class RecordController {
     @Autowired
     RecordServiceImpl recordService;
     @Autowired
     AdUserServiceImpl adUserService;
     @Autowired
     RecordMapper recordMapper;
     @Autowired
     PayMapper payMapper;
     @Autowired
     MtUserServiceImpl mtUserService;


    /**
     * 通过类型查找各种记录表
     * @param recordVo
     * @return
     */
     @PostMapping("getRecordAll")
     public Result getTypeAll(@RequestBody RecordVo recordVo){
         String token=recordVo.getAdUserToken();
         if(token!=null){
             String adUserId = TokenUtilT.getTokenById(token);
             AdUser adUser= adUserService.findAdUserById(adUserId);
             if(adUser!=null){
                 return recordService.selectAll(recordVo.getPageNum(),recordVo.getPageSize(),
                         adUserId,recordVo.getStatus(),recordVo.getStartTime(),
                         recordVo.getEndTime(),recordVo.getContent(),recordVo.getGzhName(),recordVo.getThisMonth(),recordVo.getLastMonth());
             }
             return ResultUtil.error("无token");
         }
         String mttoken=recordVo.getMtUserToken();
         String mtUserId = TokenUtilT.getTokenById(mttoken);
         MtUser mtUser= mtUserService.selectByMtUserId(mtUserId);
         if(mtUser!=null){
             return recordService.selectAll(recordVo.getPageNum(),recordVo.getPageSize(),
                     mtUserId,recordVo.getStatus(),recordVo.getStartTime(),recordVo.getEndTime()
                     ,recordVo.getContent(),recordVo.getGzhName(),recordVo.getThisMonth(),recordVo.getLastMonth());
         }
         return ResultUtil.error("无token");
     }


    /**
     * 查询提现总金额，历史总金额
     * @return
     */
    @PostMapping("getAllMoney")
    public Result getHistory(@RequestBody RecordVo recordVo){
        String token=recordVo.getMtUserToken();
        String mtUserId = TokenUtilT.getTokenById(token);
        MtUser mtUser= mtUserService.selectByMtUserId(mtUserId);
        if(mtUser!=null){
           MoneySum moneySum=new MoneySum();
           Double historySum=0.00;
           Double takeSum=0.00;
           List<Record> records= recordMapper.selectAll(mtUserId,(byte)5,null,null,null,null,null,null);
           for(int i=0;i<records.size();i++){
               historySum+=records.get(i).getMoney();
           }
            BigDecimal b =new BigDecimal(historySum);
            moneySum.setHistorySum(b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue());
           List<Pay> pays= payMapper.selectByStatus(mtUserId,(byte)2);
           for(int i=0;i<pays.size();i++){
               takeSum+=pays.get(i).getMoney();
          }
            BigDecimal c =new BigDecimal(takeSum);
            moneySum.setTakeSum(c.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue());
           return ResultUtil.success(moneySum);
        }
        return ResultUtil.error("无token");
    }





}
